package com.wind.stream;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;

/**
 * Stream的使用：映射
 *
 * @author Wind
 * @date 2021-06-06
 */
public class Test05Stream {
    public static void main(String[] args) {

        // 1. 英文数组的英文全部改为大写
        String[] arrStr = {"abcd", "cdfm", "msg", "ssm"};
        List<String> list = Arrays.asList(arrStr);
        System.out.println(list);   // 原来的数组
        List<String> newList = list.stream().map(t -> t.toUpperCase(Locale.ROOT)).collect(Collectors.toList());
        System.out.println(newList);
        System.out.println(list);   // 原来的数组
        System.out.println("=============================================================");

        // 2. 整数数组每个元素+3
        List<Integer> integers = Arrays.asList(1, 2, 3, 4, 5);
        List<Integer> newIntegerList = integers.stream().map(t -> t + 3).collect(Collectors.toList());
        System.out.println(integers);
        System.out.println(newIntegerList);
        System.out.println("=============================================================");

        // 3. 将所有员工的薪资增加1000
        List<Person> persons1 = new ArrayList<>();
        persons1.add(new Person("张三", 7000, 34, "男", "陕西"));
        persons1.add(new Person("李四", 8000, 24, "男", "河北"));
        persons1.add(new Person("王五", 6000, 42, "女", "甘肃"));
        persons1.add(new Person("王五", 9000, 12, "女", "上海"));
        // 这种情况会改变原对象的值
        List<Person> newpersons1 = persons1.stream().map(t -> {
            t.setSalary(t.getSalary() + 1000);
            return t;
        }).collect(Collectors.toList());
        // 原来集合的值被修改了...因为操作的时候是原对象
        System.out.println(persons1);    // 原来集合的值
        System.out.println(newpersons1); // 新集合的值
        System.out.println("=============================================================");

        // 不改变原有集合值的情况（在修改的时候 new一个新的对象操作）
        List<Person> persons2 = new ArrayList<>();
        persons2.add(new Person("张三", 7000, 34, "男", "陕西"));
        persons2.add(new Person("李四", 8000, 24, "男", "河北"));
        persons2.add(new Person("王五", 6000, 42, "女", "甘肃"));
        persons2.add(new Person("王五", 9000, 12, "女", "上海"));
        List<Person> newPersons2 = persons2.stream().map(t -> {
            Person person = new Person(t.getName(), t.getSalary(), t.getAge(), t.getSex(), t.getArea());
            person.setSalary(t.getSalary() + 1000);
            return person;
        }).collect(Collectors.toList());
        System.out.println(persons2);
        System.out.println(newPersons2);
        System.out.println("=============================================================");

        // 4. 将两个字符数组 组合成一个新的字符数组
        List<String> strings = Arrays.asList("m-k-l-a", "1-2-4-7");
        List<String> newStrings = strings.stream().flatMap(t -> {
            String[] split = t.split("-");
            return Arrays.stream(split);
        }).collect(Collectors.toList());
        System.out.println(strings);
        System.out.println(newStrings);
    }
}
